package clientAPI;
import java.io.Serializable;
import java.util.Date;

/**
 * ServerMessage is an object that is sent as a reply to the client.
 * @author jonathan
 *
 */
public class ServerMessage implements Serializable {
	private static final long serialVersionUID = 1L;
	private String err;
	private Date date;

	/* mapVal contains the value of the map on a specific position */
	private Integer mapVal;
	/* mapX and mapY are the positions in the array where this update happens */
	private int mapX, mapY;
	
	
	/**
	 * Constructor of the message.
	 * @param v The value to set
	 * @param x the x-position to set the value on
	 * @param y the y-position to set the value on
	 */
	public ServerMessage(Integer v, int x, int y) {
		mapVal = v;
		mapX = x;
		mapY = y;
		x=0;y=0;
		date = new java.util.Date();
	}
	
	/**
	 * If an error message need be sent to the client, set this flag.
	 * @param error The error message, use capital letters!
	 */
	public void setError(String error) {
		// We might want to compile an enumeration of the most common errors later
		err = error;
	}
	public String getErr() { return err; }

	/** Get the timestamp of the message */
	public Date getTimestamp() { return date; }
	
	public Integer getMapValue() {
		return mapVal;
	}
	public int getMapValueXPosition() {
		return mapX;
	}
	public int getMapValueYPosition() {
		return mapY;
	}
	
	@Override public String toString() {
	    StringBuilder result = new StringBuilder();
	    result.append(this.getClass().getName() + " (" + date + "): ");
	    result.append("Position: (" + mapX + "," + mapY + ") has value " + mapVal + ".");

	    if (err != "") {
	    	result.append("ERROR: " + err);
	    }
	    return result.toString();
	}
}
